package rexsee.natives;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;

public class RexseeJavaDialog implements JavascriptInterface {

	public static final String INTERFACE_NAME = "JavaDialog";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return this;
	}

	public static final String ERROR_DIALOG_NOT_EXISTS = "The dialog specified by the id does not exist.";

	private final Browser mBrowser;
	private final Context mContext;
	private final HashMap<String, JavaDialog> mMap;
	private final RexseeJavaView mViews;
	private boolean syncTag = false;

	public RexseeJavaDialog(Browser browser, RexseeJavaView views) {

		super();
		mBrowser = browser;
		mContext = browser.getContext();
		mMap = new HashMap<String, JavaDialog>();
		mViews = views;

		browser.eventList.add(JavaDialog.EVENT_ONJAVADIALOGDISMISSED);

	}

	public void _remove(String id) {
		if (!mMap.containsKey(id)) return;
		final JavaDialog dialog = mMap.get(id);
		if (dialog == null) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					dialog.removeChilds();
					dialog.dismiss();
				} catch (Exception e) {
					mBrowser.exception(getInterfaceName(), e);
				}
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
	}

	//JavaScript Interface

	public void show(final String id, final String style) {
		if (id == null || id.trim().equalsIgnoreCase("")) {
			mBrowser.exception(getInterfaceName(), "The id of dialog should not be null.");
			return;
		}
		if (mMap.containsKey(id)) {
			mBrowser.exception(getInterfaceName(), "The specified view has already been shown.");
			return;
		}
		final ViewTag viewTag = mViews.getViewTag(id);
		if (viewTag == null) {
			mBrowser.exception(getInterfaceName(), "The specified view is null.");
			return;
		}
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					final JavaDialog dialog = new JavaDialog(mBrowser, style, viewTag);
					mMap.put(id, dialog);
					dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dlg) {
							try {
								dialog.removeChilds();
								mMap.remove(id);
								mBrowser.eventList.run(JavaDialog.EVENT_ONJAVADIALOGDISMISSED, new String[]{id});
							} catch (Exception e) {
							}
						}
					});
					dialog.start();
				} catch (Exception e) {
					mBrowser.exception(getInterfaceName(), e);
				}
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
		return;
	}
	public void dismiss(String id) {
		_remove(id);
		mMap.remove(id);
		return;
	}
	public void dismissAll() {
		Set<String> keys = mMap.keySet();
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext())
			_remove(iterator.next());
		mMap.clear();
	}
	public void setStyle(String id, String style) {
		if (id == null || id.trim().equalsIgnoreCase("") || !mMap.containsKey(id)) {
			mBrowser.exception(getInterfaceName(), "The dialog specified by the id does not exist.");
			return;
		}
		mMap.get(id).setStyle(style);
	}
	public String getStyle(String id, String name) {
		return (!mMap.containsKey(id)) ? "" : mMap.get(id).getStyle(name);
	}
	public boolean exists(String id) {
		return mMap.containsKey(id);
	}
	public int size() {
		return mMap.size();
	}
	public String getIds() {
		String rtn = "";
		Set<String> ids = mMap.keySet();
		Iterator<String> iterator = ids.iterator();
		while (iterator.hasNext()) {
			if (!rtn.equals("")) rtn += ",";
			rtn += "\"" + iterator.next() + "\"";
		}
		return "[" + rtn + "]";
	}

}
